package com.learn.metrics.type.meter;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import com.codahale.metrics.Meter;
import com.learn.metrics.util.MetricUtil;

/**
 * 速率统计
 * <p>比如TPS。Meters会统计最近1分钟，5分钟，15分钟，还有全部时间的速率。</p>
 * @author XuYang
 * @since 2025/2/18 21:31
 */
public class MeterExample {

    public static void start(Integer interval) {
        MetricUtil.startReporter(interval);
        Meter requests = MetricUtil.REGISTRY.meter("requests");
        Meter responses = MetricUtil.REGISTRY.meter("response");
        while (true) {
            if (requests.getCount() <= 10) {
                requests.mark();
                responses.mark();
            } else {
                break;
            }
            ThreadUtil.sleep(RandomUtil.randomInt(500, 1000));
        }
    }
}
